/**
 * 用户图片设置页面
 * Created by mrcode on 2016/1/31.
 */
$(function () {
    $(function () {
        initGrxx();
        initGrtx();
    });
});

//个人形象初始化
function initGrxx() {
    $("#img-grxx-set").on('click', function () { //打开窗口
        $('#grxx-modal').modal({backdrop: true});
    });
    //初始化 图片上传插件
    var $grxx = $('#grxx-cropper');
    $grxx.cropit({
        imageBackground: true,
    });

    //原生file上传插件隐藏了，给新的按钮添加触发事件
    $("#gxxx_fileInput_btn").on("click", function () {
        $("#gxxx_fileInput").click();
    });

    //绑定提交
    $("#grxxSubmit").on("click", function () {
        grxxSubmit($grxx);
    });
}

function grxxSubmit(grxx) {
    var imageData = grxx.cropit('export');
    imageData = imageData.replace(/^data:image\/(png|jpg);base64,/, "");
    var fileData = createFileData(imageData, "pbn.png");
    var formdata = new FormData();
    formdata.append("grxx", fileData);
    $.ajax({
        type: 'POST',
        url: $contextPath + "/auth/user/blogshow_submit",
        data: formdata,
        //contentType: "application/x-www-form-urlencoded; charset=utf-8",
        processData: false,
        contentType:false,
        success: function (data) {
            var suc = data.success;
            var message = data.message;
            if (suc == 0) { //失败
                MrcodeUi.Alert.Dismissible.Plane.err("#notice",message);
            } else { //成功
                MrcodeUi.Alert.Dismissible.Plane.success("#notice",message);
                setTimeout(function () {
                    location.reload();
                }, "2000")
            }
        }
    });
}

/** 获取文件对象 */
function createFileData(imgData, fileName) {
    var blobBin = atob(imgData);
    var array = [];
    for (var i = 0; i < blobBin.length; i++) {
        array.push(blobBin.charCodeAt(i));
    }
    var blob = new Blob([new Uint8Array(array)], {type: 'image/png'});
    var file = new File([blob], fileName, {type: 'image/png'});

    var size = file.size;
    return file;
}

function initGrxx() {
    $("#img-grxx-set").on('click', function () { //打开窗口
        $('#grxx-modal').modal({backdrop: true});
    });
    //初始化 图片上传插件
    var $grxx = $('#grxx-cropper');
    $grxx.cropit({
        imageBackground: true,
    });

    //原生file上传插件隐藏了，给新的按钮添加触发事件
    $("#gxxx_fileInput_btn").on("click", function () {
        $("#gxxx_fileInput").click();
    });

    //绑定提交
    $("#grxxSubmit").on("click", function () {
        grxxSubmit($grxx);
    });
}


function initGrtx() {
    $("#img-grtx-set").on('click', function () { //打开窗口
        $('#grtx-modal').modal({backdrop: true});
    });
    //初始化 图片上传插件
    var $grtx = $('#grtx-cropper');
    $grtx.cropit({
        imageBackground: true,
    });

    //原生file上传插件隐藏了，给新的按钮添加触发事件
    $("#grtx_fileInput_btn").on("click", function () {
        $("#grtx_fileInput").click();
    });

    //绑定提交
    $("#grtxSubmit").on("click", function () {
        grtxSubmit($grtx);
    });


}

function grtxSubmit(grtx) {
    var imageData = grtx.cropit('export');
    imageData = imageData.replace(/^data:image\/(png|jpg);base64,/, "");
    var fileData = createFileData(imageData, "pbn.png");
    var formdata = new FormData();
    formdata.append("grtx", fileData);
    $.ajax({
        type: 'POST',
        url: $contextPath + "/auth/user/setavator_submit",
        data: formdata,
        //contentType: "application/x-www-form-urlencoded; charset=utf-8",
        processData: false,
        contentType:false,
        success: function (data) {
            var suc = data.success;
            var message = data.message;
            if (suc == 0) { //失败
                MrcodeUi.Alert.Dismissible.Plane.err("#grtx_notice",message);
            } else { //成功
                MrcodeUi.Alert.Dismissible.Plane.success("#grtx_notice",message);
                setTimeout(function () {
                    location.reload();
                }, "2000")
            }
        }
    });
}